capture log close
log using replication.log, replace

use replication.dta, clear

* Generate some regressors:
egen stateyear=group(state year)
egen st=group(state)
egen sty=group(state year)
gen dD1=1/D1
gen dD6=1/D6

* Table 2: benchmark results
foreach var in "TVtotS" "TQtotS" {
	ivreghdfe y (Llnsum4`var' = c.LlnTOTsum4`var'#(c.dD1 c.dD6 c.lnland c.HIDTA)), absorb(state year) cluster(state) first
}

foreach var in "TVtotS" "TQtotS" {
	ivreghdfe y (Llnsum4`var' = c.LlnTOTsum4`var'#(c.dD1 c.dD6 c.lnland c.HIDTA)), absorb(stateyear) cluster(state) first
}

* Table 3: quantification exercise
* From no transfer to 10th percentile
foreach var in "TVtotS" "TQtotS" {
	capture drop x* inst*
	gen x=Llnsum4`var'
	capture drop x2
	gen x2=Llnsum4`var'
	gen inst=LlnTOTsum4`var'
	quietly: ivreghdfe y (x = c.inst#(c.dD1 c.dD6 c.lnland c.HIDTA)), absorb(state year) cluster(state)
	* Calculate percentiles by year
	predict p if e(sample)
	forvalues i=2007(1)2016 {
		quietly: sum x if e(sample) & x>0 & year==`i', detail
		replace x=r(p10) if year==`i'
	}
	
	* Significance, using margins & contrast with the need of ivreg2
	ivreg2 y (x2 = c.inst#(c.dD1 c.dD6 c.lnland c.HIDTA)) i.st i.year if year>2006 & p~=., cluster(state) small
	margins if x2==0,  at((asobserved) _all) at(x2=generate(x)) contrast(atcontrast(r._at))
	drop p
}

foreach var in "TVtotS" "TQtotS" {
	capture drop x* inst*
	gen x=Llnsum4`var'
	capture drop x2
	gen x2=x
	gen inst=LlnTOTsum4`var'
	ivreghdfe y (x = c.inst#(c.dD1 c.dD6 c.lnland c.HIDTA)), absorb(stateyear) cluster(state)
	* Calculate percentiles by year:
	predict p if e(sample)
	forvalues i=2007(1)2016 {
		quietly: sum x if e(sample) & x>0 & year==`i', detail
		replace x=r(p10) if year==`i'
	}
	
	* Significance, using margins & contrast with the need of ivreg2
	ivreg2 y (x2 = c.inst#(c.dD1 c.dD6 c.lnland c.HIDTA)) i.sty if year>2006 & p~=., cluster(state) small
	margins if x2==0,  at((asobserved) _all) at(x2=generate(x)) contrast(atcontrast(r._at))
	drop p
}

* From no transfer to 25th percentile
foreach var in "TVtotS" "TQtotS" {
	capture drop x* inst*
	gen x=Llnsum4`var'
	capture drop x2
	gen x2=Llnsum4`var'
	gen inst=LlnTOTsum4`var'
	quietly: ivreghdfe y (x = c.inst#(c.dD1 c.dD6 c.lnland c.HIDTA)), absorb(state year) cluster(state)
	* Calculate percentiles by year
	predict p if e(sample)
	forvalues i=2007(1)2016 {
		quietly: sum x if e(sample) & x>0 & year==`i', detail
		replace x=r(p25) if year==`i'
	}
	
	* Significance, using margins & contrast with the need of ivreg2
	ivreg2 y (x2 = c.inst#(c.dD1 c.dD6 c.lnland c.HIDTA)) i.st i.year if year>2006 & p~=., cluster(state) small
	margins if x2==0,  at((asobserved) _all) at(x2=generate(x)) contrast(atcontrast(r._at))
	drop p
}

foreach var in "TVtotS" "TQtotS" {
	capture drop x* inst*
	gen x=Llnsum4`var'
	capture drop x2
	gen x2=x
	gen inst=LlnTOTsum4`var'
	ivreghdfe y (x = c.inst#(c.dD1 c.dD6 c.lnland c.HIDTA)), absorb(stateyear) cluster(state)
	* Calculate percentiles by year:
	predict p if e(sample)
	forvalues i=2007(1)2016 {
		quietly: sum x if e(sample) & x>0 & year==`i', detail
		replace x=r(p25) if year==`i'
	}
	
	* Significance, using margins & contrast with the need of ivreg2
	ivreg2 y (x2 = c.inst#(c.dD1 c.dD6 c.lnland c.HIDTA)) i.sty if year>2006 & p~=., cluster(state) small
	margins if x2==0,  at((asobserved) _all) at(x2=generate(x)) contrast(atcontrast(r._at))
	drop p
}


* Table 4
* Panel A: size of counties
foreach var in "TVtotS" "TQtotS" {
	capture drop x*
	gen x=Llnsum4`var'
	gen x_pop=x*tot_pop
	ivreghdfe y (x x_pop = c.LlnTOTsum4`var'#(c.dD1 c.dD6 c.lnland c.HIDTA)##c.tot_pop) tot_pop, absorb(state year) cluster(state)
	sum tot_pop if e(sample), detail
	test x+x_pop*0.487317=0
}

foreach var in "TVtotS" "TQtotS" {
	capture drop x*
	gen x=Llnsum4`var'
	gen x_pop=x*tot_pop
	ivreghdfe y (x x_pop = c.LlnTOTsum4`var'#(c.dD1 c.dD6 c.lnland c.HIDTA)##c.tot_pop) tot_pop, absorb(stateyear) cluster(state)
	sum tot_pop if e(sample), detail
	test x+x_pop*0.474768 =0
}

* Panel B: newspaper availability
foreach var in "TVtotS" "TQtotS" {
	capture drop x* 
	gen x=Llnsum4`var'
	gen x_newsd=x*newsd
	ivreghdfe y (x x_newsd = c.LlnTOTsum4`var'#(c.dD1 c.dD6 c.lnland c.HIDTA)##i.newsd) newsd, absorb(state year) cluster(state)
	test x+x_newsd=0
}

foreach var in "TVtotS" "TQtotS" {
	capture drop x* 
	gen x=Llnsum4`var'
	gen x_newsd=x*newsd
	ivreghdfe y (x x_newsd = c.LlnTOTsum4`var'#(c.dD1 c.dD6 c.lnland c.HIDTA)##i.newsd) newsd, absorb(stateyear) cluster(state)
	test x+x_newsd=0
}

* Panel C: Democratic-leaning counties
label var D_share_All "Dem share in previous election"
foreach var in "TVtotS" "TQtotS" {
	capture drop x*
	gen x=Llnsum4`var'
	gen x_dem=x*D_share_All
	ivreghdfe y (x x_dem = c.LlnTOTsum4`var'#(c.dD1 c.dD6 c.lnland c.HIDTA)##c.D_share_All) D_share_All, absorb(state year) cluster(state)
	sum D_share_All if e(sample),detail
	test x+x_dem*0.5019286=0
}

foreach var in "TVtotS" "TQtotS" {
	capture drop x*
	gen x=Llnsum4`var'
	gen x_dem=x*D_share_All
	ivreghdfe y (x x_dem = c.LlnTOTsum4`var'#(c.dD1 c.dD6 c.lnland c.HIDTA)##c.D_share_All) D_share_All, absorb(stateyear) cluster(state)
	sum D_share_All if e(sample),detail
	test x+x_dem*0.5010982=0
}

* Table 5: Adding control variables
* Panel A: socio-economic controls
foreach var in "TVtotS" "TQtotS" {
	ivreghdfe y (Llnsum4`var' = c.LlnTOTsum4`var'#(c.dD1 c.dD6 c.lnland c.HIDTA)) MedianHouseholdIncome tot_pop shareblack past_share crime, absorb(state year) cluster(state)
}

foreach var in "TVtotS" "TQtotS" {
	ivreghdfe y (Llnsum4`var' = c.LlnTOTsum4`var'#(c.dD1 c.dD6 c.lnland c.HIDTA)) MedianHouseholdIncome tot_pop shareblack past_share crime, absorb(stateyear) cluster(state)
}

* Panel B: distance from closest military base
foreach var in "TVtotS" "TQtotS" {
	ivreghdfe y (Llnsum4`var' = c.LlnTOTsum4`var'#(c.dD1 c.dD6 c.lnland c.HIDTA)) i.year#c.dD1mil if year>2006 & year~=2009 & year~=2013, absorb(state year) cluster(state)
}

foreach var in "TVtotS" "TQtotS" {
	ivreghdfe y (Llnsum4`var' = c.LlnTOTsum4`var'#(c.dD1 c.dD6 c.lnland c.HIDTA)) i.year#c.dD1mil if year>2006 & year~=2009 & year~=2013, absorb(stateyear) cluster(state)
}

* Table 6: Other dependent variables
* Panel A: change in vote share
foreach var in "TVtotS" "TQtotS" {
	ivreghdfe change_share (Llnsum4`var' = c.LlnTOTsum4`var'#(c.dD1 c.dD6 c.lnland c.HIDTA)), absorb(state year) cluster(state) 
}

foreach var in "TVtotS" "TQtotS" {
	ivreghdfe change_share (Llnsum4`var' = c.LlnTOTsum4`var'#(c.dD1 c.dD6 c.lnland c.HIDTA)), absorb(stateyear) cluster(state) 
}

* Panel B: change in votes for incumbent
foreach var in "TVtotS" "TQtotS" {
	ivreghdfe change_votes (Llnsum4`var' = c.LlnTOTsum4`var'#(c.dD1 c.dD6 c.lnland c.HIDTA)), absorb(state year) cluster(state) 
}

foreach var in "TVtotS" "TQtotS" {
	ivreghdfe change_votes (Llnsum4`var' = c.LlnTOTsum4`var'#(c.dD1 c.dD6 c.lnland c.HIDTA)), absorb(stateyear) cluster(state) 
}

* Panel C: change in total votes
foreach var in "TVtotS" "TQtotS" {
	ivreghdfe change_CASTvotes (Llnsum4`var' = c.LlnTOTsum4`var'#(c.dD1 c.dD6 c.lnland c.HIDTA)), absorb(state year) cluster(state) 
}

foreach var in "TVtotS" "TQtotS" {
	ivreghdfe change_CASTvotes (Llnsum4`var' = c.LlnTOTsum4`var'#(c.dD1 c.dD6 c.lnland c.HIDTA)), absorb(stateyear) cluster(state) 
}

* Table 7: Robustness checks on regressor
* Panel A: equipment received over past 2 years
foreach var in "TVtotS" "TQtotS" {
	ivreghdfe y (Llnsum2`var' = c.LlnTOTsum2`var'#(c.dD1 c.dD6 c.lnland c.HIDTA)), absorb(state year) cluster(state)
}

foreach var in "TVtotS" "TQtotS" {
	ivreghdfe y (Llnsum2`var' = c.LlnTOTsum2`var'#(c.dD1 c.dD6 c.lnland c.HIDTA)), absorb(stateyear) cluster(state)
}

* Panel B: equipment received over all past years
foreach var in "TVtotS" "TQtotS" {
	ivreghdfe y (Llnsum`var' = c.LlnTOTsum`var'#(c.dD1 c.dD6 c.lnland c.HIDTA)), absorb(state year) cluster(state)
}

foreach var in "TVtotS" "TQtotS" {
	ivreghdfe y (Llnsum`var' = c.LlnTOTsum`var'#(c.dD1 c.dD6 c.lnland c.HIDTA)), absorb(stateyear) cluster(state)
}

* Panel C: equipment received by any agency over the past 4 past years
foreach var in "TVtot" "TQtot" {
	ivreghdfe y (Llnsum4`var' = c.LlnTOTsum4`var'#(c.dD1 c.dD6 c.lnland c.HIDTA)), absorb(state year) cluster(state)
}

foreach var in "TVtot" "TQtot" {
	ivreghdfe y (Llnsum4`var' = c.LlnTOTsum4`var'#(c.dD1 c.dD6 c.lnland c.HIDTA)), absorb(stateyear) cluster(state)
}

* Panel D: indicator variable for transfers received
foreach var in "TVtotS" "TQtotS" {
	ivreghdfe y (received = c.LlnTOTsum4`var'#(c.dD1 c.dD6 c.lnland c.HIDTA)), absorb(state year) cluster(state)
}

foreach var in "TVtotS" "TQtotS" {

	ivreghdfe y (received = c.LlnTOTsum4`var'#(c.dD1 c.dD6 c.lnland c.HIDTA)), absorb(stateyear) cluster(state)
}

* Table 8: Results with county fixed effects
foreach var in "TVtotS" "TQtotS" {
	ivreghdfe y (Llnsum4`var' = LlnTOTsum4`var' c.LlnTOTsum4`var'#(c.dD1 c.dD6 c.lnland c.HIDTA)) if year>2006, absorb(countyFIPS decade) cluster(state)
}

* Table 9: Robustness checks on instruments
* Panel A: adding non-time varying instrument
foreach var in "TVtotS" "TQtotS" {
	ivreghdfe y (Llnsum4`var' = dD1 dD6 lnland HIDTA c.LlnTOTsum4`var'#(c.dD1 c.dD6 c.lnland c.HIDTA)) if year>2006 & year~=2009 & year~=2013, absorb(state year) cluster(state)
}

foreach var in "TVtotS" "TQtotS" {
	ivreghdfe y (Llnsum4`var' = dD1 dD6 lnland HIDTA c.LlnTOTsum4`var'#(c.dD1 c.dD6 c.lnland c.HIDTA)) if year>2006 & year~=2009 & year~=2013, absorb(stateyear) cluster(state)
}

* Panel B: instruments only in level
foreach var in "TVtotS" "TQtotS" {
	ivreghdfe y (Llnsum4`var' = dD1 dD6 lnland HIDTA) if year>2006 & year~=2009 & year~=2013, absorb(state year) cluster(state)
}

foreach var in "TVtotS" "TQtotS" {
	ivreghdfe y (Llnsum4`var' = dD1 dD6 lnland HIDTA) if year>2006 & year~=2009 & year~=2013, absorb(stateyear) cluster(state)
}

* Panel C: using Masera’s (2020, 2021) formulation
gen D1mil_dummy=0 if dD1mil~=.
replace D1mil_dummy=1 if D1mil<12.5 /* we have miles */
gen D1_dummy=0 if dD1~=.
replace D1_dummy=1 if D1<12.5 /* we have miles */
gen D6_dummy=0 if dD6~=.
replace D6_dummy=1 if D6<12.5 /* we have miles */

foreach var in "TVtotS" "TQtotS" {
	ivreghdfe y (Llnsum4`var' = c.eqp_revA#(c.D1_dummy c.D6_dummy c.lnland c.HIDTA)) i.year#c.D1mil_dummy, absorb(state year) cluster(state)
}

foreach var in "TVtotS" "TQtotS" {
	ivreghdfe y (Llnsum4`var' = c.eqp_revA#(c.D1_dummy c.D6_dummy c.lnland c.HIDTA)) i.year#c.D1mil_dummy, absorb(stateyear) cluster(state)
}

* Panel D: adding Bove and Gravilova (2017) instrument
foreach var in "TVtotS" "TQtotS" {
	ivreghdfe y (Llnsum4`var' = c.LlnTOTsum4`var'#(c.dD1 c.dD6 c.lnland c.HIDTA) BGinst), absorb(state year) cluster(state)
}

foreach var in "TVtotS" "TQtotS" {
	ivreghdfe y (Llnsum4`var' = c.LlnTOTsum4`var'#(c.dD1 c.dD6 c.lnland c.HIDTA) BGinst), absorb(stateyear) cluster(state)
}

* Table A1: OLS results
foreach var in "TVtotS" "TQtotS" {
	ivreghdfe y Llnsum4`var', absorb(state year) cluster(state) 
}

foreach var in "TVtotS" "TQtotS" {
	ivreghdfe y Llnsum4`var', absorb(stateyear) cluster(state)
}

clear
log close
